/* 
 *  Copyright 2012 CodeMagi, Inc.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.codemagi.login.policy;

import com.codemagi.login.model.IUser;
import com.codemagi.servlets.AppException;
import com.codemagi.util.*;
import java.util.List;

public class PasswordCharacterSetPolicy implements IPasswordPolicy {

    /**
     * Alphabet consisting of the lower case letters A-Z.
     */
    public static final char[] LOWERCASE_LETTERS_ALPHABET = {
	'a','b','c','d','e','f','g','h',
	'i','j','k','l','m','n','o','p',
	'q','r','s','t','u','v','w','x',
	'y','z',
    };

    /**
     * Alphabet consisting of the upper case letters A-Z.
     */
    public static final char[] UPPERCASE_LETTERS_ALPHABET = {
	'A','B','C','D','E','F','G','H',
	'I','J','K','L','M','N','O','P',
	'Q','R','S','T','U','V','W','X',
	'Y','Z',
    };

    /**
     * Alphabet consisting of the base-10 numbers
     */
    public static final char[] NUMBERS_ALPHABET = {
	'1','2','3','4','5','6','7','8','9','0'
    };

    /**
     * Alphabet consisting of all the printable ASCII symbols.
     */
    public static final char[] SYMBOLS_ALPHABET = {
	'!','\"','#','$','%','&','\'','(',
	')','*','+',',','-','.','/',':',
	';','<','?','@','[','\\',']','^',
	'_','`','{','|','}','~',
    };
    
    private Integer numRequired        = null;
    private List<char[]> characterSets = null;
    private String errorMessage; 
    
    public PasswordCharacterSetPolicy(List<char[]> characterSets) {
	this.characterSets = characterSets;

	numRequired        = characterSets.size();

	StringBuffer msgBuffer = new StringBuffer(512);
	msgBuffer.append("Your password must contain characters from all of the following sets:<br>");

	for (char[] set : characterSets) {
	    msgBuffer.append( ArrayUtils.delimit(set, ",") )
		.append("<br>");
	}

	errorMessage = msgBuffer.toString();
    }

    public PasswordCharacterSetPolicy(List<char[]> characterSets, Integer numRequired) {
	this.characterSets = characterSets;
	this.numRequired   = numRequired;

	StringBuffer msgBuffer = new StringBuffer(512);
	msgBuffer.append("Your password must contain characters from ")
	    .append( numRequired )
	    .append(" of the following sets:<br>");

	for (char[] set : characterSets) {
	    msgBuffer.append( ArrayUtils.delimit(set, ",") )
		.append("<br>");
	}

	errorMessage = msgBuffer.toString();
    }


    /** 
     * Actually performs the policy check
     */
    public boolean check(IUser user, String newPassword) throws AppException {

        if (Utils.isEmpty(newPassword)) throw new AppException("Password is null");

	int matchCount = 0;

	for (char[] set : characterSets) {
	    for (char c : set) {
		if (newPassword.contains(c+"")) {
		    matchCount++;
		    break;
		}
	    }
	}

	if (matchCount < numRequired) throw new AppException(errorMessage);
	    
	return true;
    }

}
